#!/usr/bin/python
# -*-coding:utf-8 -*-

"""Bilibili Toolkit 哔哩哔哩工具箱"""

banner = r"""
        \\         //
         \\       //
    #####################     ________   ___   ___        ___   ________   ___   ___        ___
    ##                 ##    |\   __  \ |\  \ |\  \      |\  \ |\   __  \ |\  \ |\  \      |\  \
    ##    //     \\    ##    \ \  \|\ /_\ \  \\ \  \     \ \  \\ \  \|\ /_\ \  \\ \  \     \ \  \
    ##   //       \\   ##     \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \
    ##                 ##      \ \  \|\  \\ \  \\ \  \____ \ \  \\ \  \|\  \\ \  \\ \  \____ \ \  \
    ##       www       ##       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\
    ##                 ##        \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|
    #####################
        \/         \/                               哔哩哔哩 (゜-゜)つロ 干杯~
"""

import time
import json
import pickle
import os
import datetime
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
class Bilibili:
    def __init__(self,platform):
        print("__init__......")
        self.cookies =None
        #登录参数 sudo yum update google-chrome-stable  google-chrome-stable.x86_64 0:81.0.4044.129-1 
        chrome_opt = webdriver.ChromeOptions()
        #window登录参数
        if platform == 'window':
            path = r".\chromedriver\chromedriver.exe"
        else:
            path = r"./chromedriver/chromedriver"
            chrome_opt.add_argument("--headless")  # Runs Chrome in headless mode.
            chrome_opt.add_argument('--no-sandbox')  # Bypass OS security model
        # applicable to windows os only
        chrome_opt.add_argument('--disable-gpu')
        # chrome_opt.add_argument('start-maximized')
        chrome_opt.add_argument('disable-infobars')
        chrome_opt.add_argument("--disable-extensions")
        chrome_opt.add_argument('--disable-dev-shm-usage')
        chrome_opt.add_argument('window-size=1920x1480')
        #登录初始化
        self.browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_opt)
    def login(self):
        #产生cookies文件
        self.browser.get("https://live.bilibili.com/21954555")
        time.sleep(65)
        cookies = self.browser.get_cookies()
        print("wait 60 .............")
        pickle.dump(cookies, open("cookies.pkl","wb"))
        print(cookies, "login succes")
    
    def loginWithCookies(self):
        """
        cookies login
        """
        print("loginWithCookies......")
        #if os.path.isfile("D:\local\Programs\Python\Python38-32\cookies.pkl")==False:
           # self.login()
		# 指定的文件存在
        self.cookies=pickle.load(open("cookies.pkl", "rb"))
        print("load begin............")
        self.browser.get("https://live.bilibili.com/21954555")
        for cookie in self.cookies:
            if 'expiry' in cookie:
              cookie['expiry'] = int(cookie['expiry'])
            self.browser.add_cookie(cookie)
        time.sleep(10)
        print("load end...........")
        self.browser.refresh()
    def postMsg(self):
        """
        弹幕
        """
        var=0
        while var < 666: 
            var =var+1
            # 内容框
            print("postMsg......begin")
            self.browser.get("https://live.bilibili.com/21954555")
            time.sleep(60)
            contentButtion = WebDriverWait(self.browser, 10).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, ".chat-input.border-box")))
            print("contentButtion......end")
            msg ="专注提醒 第"+str(var) +"次"+"2 5分钟工作，5分钟休息,不熬夜，不加班"
            contentButtion.send_keys(msg)
            time.sleep(1)
            #按钮
            # CSS选择器 https://www.w3school.com.cn/cssref/css_selectors.asp
            sendButtion = WebDriverWait(self.browser, 5).until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, ".bl-button.live-skin-highlight-button-bg.bl-button--primary.bl-button--small")))
            sendButtion.click()
            print("sendButtion.click end")
            #补充说明
            msg ="不熬夜，不加班,不拖延"
            contentButtion.send_keys(msg)
            time.sleep(1)
            sendButtion.click()            
            time.sleep(60)
    def close(self):
        self.browser.quit()
    def closetab(self):
        self.browser.close()
if __name__ == '__main__':
    bb = Bilibili("centos")
    #bb.login()
    bb.loginWithCookies()
    bb.postMsg()
    bb.close()


